package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbOperator.AdvancedOperation;

/**
 * Servlet implementation class Reader
 */
@WebServlet("/Reader")
public class Reader extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**这个函数根据参数判断是哪一个页面发来的请求，调用相应的函数对reader进行操作
	 * @param request 客户端的请求
	 * @param response 给客户端的响应
	 * @throws ServletException
	 * @throws IOException
     * makisemirai注释
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int type = Integer.parseInt(request.getParameter("Type"));
		
		try {
			switch(type) {
			case 1:{editReader(request, response);break;}
			case 2:{registerReader(request, response);break;}
			case 3:{deleteReader(request, response);break;} }
			}	catch (SQLException e) {
			e.printStackTrace();} 
		response.sendRedirect(request.getHeader("Referer"));
	}

    /**这个函数用于登记添加reader
	 * @param request 客户端的请求
	 * @param response 给客户端的响应
	 * @throws SQLException
	 * @throws IOException
     * makisemirai注释
	 */
	public void registerReader(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			response.sendRedirect("index.html");
			return;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				Connection con = dbOperator.BasicOperation.getConnection();
				int uid = AdvancedOperation.getUidbyCookie(con, currentCookie);
				int uidid=AdvancedOperation.getIdentity(con,uid);
				String Email=request.getParameter("Email");
				String PhoneNumber=request.getParameter("PhoneNumber");
				dbOperator.AdvancedOperation.registerNewUser(Email,PhoneNumber, uidid, 3);
				dbOperator.AdvancedOperation.addDeposit(con, Email);
				con.close();
			}
		}
	}

    /**这个函数用于编辑reader的信息根据UID定位
	 * @param request 客户端的请求
	 * @param response 给客户端的响应
	 * @throws SQLException
	 * @throws IOException
     * makisemirai注释
	 */
	public void editReader(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			response.sendRedirect("index.html");
			return;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				Connection con = dbOperator.BasicOperation.getConnection();
				int uid = AdvancedOperation.getUidbyCookie(con, currentCookie);
				if(AdvancedOperation.getIdentity(con,uid)!=2)return;
				String eUID=request.getParameter("eUID");
				if(eUID==null)return;
				String Email=request.getParameter("Email");
				String PhoneNumber=request.getParameter("PhoneNumber");
				int euid=Integer.parseInt(eUID);
				Statement sql = con.createStatement();
				sql.executeUpdate("update User set Email='"+ 
						Email+ "',PhoneNumber='" + PhoneNumber+ "' where uid ="+euid);
				con.close();
			}
		}
	}

    /**此函数根据UID定位删除reader
	 * @param request 客户端的请求
	 * @param response 给客户端的响应
	 * @throws ServletException
	 * @throws IOException
	 * @throws SQLException
     * makisemirai注释
	 */
	protected void deleteReader(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			response.sendRedirect("index.html");
			return;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				Connection con = dbOperator.BasicOperation.getConnection();
				int uid = AdvancedOperation.getUidbyCookie(con, currentCookie);
				String readeridstr = request.getParameter("dUID");
				if (readeridstr == null)
					return;
				int readerid = Integer.parseInt(readeridstr);
				AdvancedOperation.getReaderDeleted(con, uid, readerid);
				AdvancedOperation.getDepositDeleted(con, readerid);
				con.close();
			}
		}
	}
}
